package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func worker(ctx context.Context) {
LOOP:
	for {

		select {
		case <-ctx.Done():
			break LOOP
		default:
			fmt.Println("still workiing!")
		}
		// 暂停1秒
		time.Sleep(1 * time.Second)
	}
	wg.Done()

}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	wg.Add(2)
	go worker(ctx)
	go worker(ctx)
	// 主线程等待5秒
	time.Sleep(time.Second * 5)
	// 调用cancel 来取消
	cancel()
	wg.Wait()
	fmt.Println("over")
}
